<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    String _id=request.getParameter("_");
%>
<form id="form_dict_input" class="form-horizontal">
    <input type="hidden" name="keyId" id="keyId" value="${id}"/>
    <div id="container_dict_input"></div>
</form>

<script id="templates_dict_input" type="text/x-jsrender">

    <div class="form-group">
        <label class="col-md-3 control-label">系统字典</label>
        <div class="col-md-7">
            <input type="text" name="name" id="name" value="{{:name}}" class="form-control" placeholder="请输入系统字典名称">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">关键字</label>
        <div class="col-md-7">
            <input type="text" name="value" id="value" value="{{:value}}" class="form-control" placeholder="请输入关键字">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">描述信息</label>
        <div class="col-md-7">
            <textarea name="description" id="description" rows="3" class="form-control" placeholder="请输入描述信息">{{:description}}</textarea>
        </div>
    </div>
    <div class="form-group">
	    <div class="col-sm-offset-3 col-sm-9">
	        <div class="checkbox">
	            <input type="checkbox" name="ifKey" id="ifKey" class="magic-checkbox" value="ifKey">
		        <label for="ifKey">内部字典</label>
	        </div>
		    <small class="help-block">注: 用户级系统字典配置将不会显示</small>
		</div>
    </div>

</script>

<script>

    $(function(){

        // 加载数据
        fn_dict_input_load();

    });

    function fn_dict_input_load() {
        var $templates = $.templates("#templates_dict_input");
        var $container = $("#container_dict_input");
        var keyId = $("#keyId").val();

        var url;
        if (keyId) {
            url = "<%=path%>/main/dict/read/" + keyId;
        } else {
            url = "<%=path%>/main/dict/init";
        }
        __ajax_get(url, null, function (data) {
            if (data.state == 1) {
                var entity = data.data;

                // 初始化模板
                var html = $templates.render(entity);
                $container.html(html);

                // 表单数据校验
                fn_dict_input_validate();

                $('#ifKey').attr('checked', entity.ifKey == 1);
            }
        });
    }

    // 表单数据校验
    function fn_dict_input_validate() {
        __init_validate('form_dict_input',{
            rules : {
                name : {
                    required : true
                },
                value : {
                    required : true
                }
            },
            messages : {
                name : {
                    required : '请输入系统字典名称'
                },
                value : {
                    required : '请输入关键字'
                }
            }
        });
    }

    function fn_dict_input_save(callback) {
        if (!$("#form_dict_input").valid()) {
            return false;
        }
        __confirm_dialog(null, '确定提交么?',
                function () {
                    __form_save("form_dict_input", "<%=path%>/main/dict/save", function(data) {
                        if(data.state == 1){
                            callback(data.data);
                        } else {
                            callback();
                        }
                    });
                },
                function () {
                    callback();
                }
        );
    }

</script>